<?php
namespace app\admin\controller;

use app\model\Enter as EnterModel;
class EnterController extends Base {

    public function index()
    {
        $page = input('post.page', 1, 'intval');
        $limit = input('post.limit', 10, 'intval');
        $state = input('post.state');
        $offset = ($page - 1) * $limit;
        $map = ['status'=>1];
        $map['state'] = $state;

        $item = EnterModel::where($map)->limit($offset, $limit)->select();
        $count = EnterModel::where($map)->count();
        if(!empty($item) ){
            $item = $item->toArray();
            foreach ($item as $key => $value ){
                $item[$key]['state'] = $value['state'] == 0 ? '未审核' : ($value['state'] == 1 ? '审核通过' : '审核失败' );
            }

        }


        $data = [
            'code' => 0,
            'msg' => '',
            'data' => [
                'count' => $count,
                'item' => $item
            ]
        ];
        return $this->json($data);

    }

    public function delete()
    {
        $id = input('post.id', '', 'serach_in');
        $res = EnterModel::update(['status'=>0], ['id' => $id]);
        if($res){
            return $this->json(['msg' => '删除成功', 'error'=>200]);
        }else{
            return $this->json(['msg' => '删除失败', 'error'=>400]);
        }
    }

    public function getInfo()
    {
        $id = input('post.id', '', 'serach_in');
        $res = EnterModel::find($id);
        return $this->json(['data' => $res]);
    }

    public function update()
    {
        $post = input('post.', '', 'serach_in');
        $state = EnterModel::update([
            'state' => $post['state'],
            'state_msg' => $post['state_msg']
        ], ['id' => $post['id']]);
        if(!$state){
            return $this->json(['msg' => '修改失败', 'error'=>400]);
        }
        return $this->json(['msg' => '修改成功', 'post'=>$post]);
    }

}